/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.jz.yanshe.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jz.com.feierman.R;



/**
 * 自定义标题栏
 * <p>
 * Java代码中: <br/>
 * <pre><code>
 * // 初始化标题栏
 * CommonTitle commonTitle = (CommonTitle) findViewById(R.id.common_title);
 * // 设置标题
 * commonTitle.setCommonTitleText(R.string.setting_msg_push);
 * // 设置左侧按钮的文案（若设置左侧按钮为图标，则文案自动清除；反之若设置按钮为文字，则图标默认清除）
 * commonTitle.setLeftText(R.string.main_record);
 * // 设置右侧按钮的图标
 * commonTitle.setRightIcon(R.mipmap.title_back);
 * // 显示右侧按钮
 * commonTitle.setRightVisiable(View.VISIBLE);
 * // 标题栏自定义监听
 * commonTitle.setOnTitleClickListener(new CommonTitle.OnTitleClickListener() {
 *    @ Override
 *    public void onCommonTitleClick(int id) {
 *        switch (id){
 *            case CommonTitle.LEFT_ID:
 *                Toast.makeText(getApplicationContext(),"按下左按钮", Toast.LENGTH_SHORT).show();
 *                break;
 *            case CommonTitle.RIGHT_ID:
 *                Toast.makeText(getApplicationContext(),"按下右按钮", Toast.LENGTH_SHORT).show();
 *                break;
 *         }
 *    }
 * });
 * </code></pre><br/>
 * xml中引用方式：
 * <pre><code>
 * &lt;com.joyoung.robot.ui.common.CommonTitle
 *     android:id="@+id/common_title"
 *     style="@style/common_titile" /&gt;
 * </code></pre>
 * </p>
 */
public class CommonTitle extends RelativeLayout {
    private TextView titleLeft, titleText, titleRight;
    private ImageView titleLeftImage, titleRightImage;
    private boolean isImageVisible = false;
    private boolean isImageVisibleRight = false;
    private OnTitleClickListener mListener;
    public static final int LEFT_ID = 1;
    public static final int MIDDLE_ID = 2;
    public static final int RIGHT_ID = 3;

    @IntDef(value = {LEFT_ID, MIDDLE_ID, RIGHT_ID})
    @Retention(RetentionPolicy.SOURCE)
    private @interface CommonTitleViewId {
    }

    public CommonTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        View rootView = LayoutInflater.from(context).inflate(R.layout.common_title, this);
        titleLeft = rootView.findViewById(R.id.common_title_left);
        titleLeftImage = rootView.findViewById(R.id.common_title_left_image);
        titleText = rootView.findViewById(R.id.common_title_text);
        titleRight = rootView.findViewById(R.id.common_title_right);
        titleRightImage = rootView.findViewById(R.id.common_title_right_image);
        // 默认右侧按钮不显示，左侧显示返回图标
        setRightVisiable(View.GONE);
//        setLeftIcon(R.mipmap.icon_home_return);
        titleLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCommonTitleClick(LEFT_ID);
                }
            }
        });
        titleLeftImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCommonTitleClick(LEFT_ID);
                }
            }
        });
        titleRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCommonTitleClick(RIGHT_ID);
                }
            }
        });
        titleRightImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCommonTitleClick(RIGHT_ID);
                }
            }
        });
    }

    /**
     * 获取标题栏上对应的按钮，对其可修改字体大小或颜色，自己去控制
     *
     * @param id 需要获取的View，可选{@link #LEFT_ID}、{@link #MIDDLE_ID}、{@link #RIGHT_ID}
     */
    public TextView getTextView(@CommonTitleViewId int id) {
        TextView tv = null;
        switch (id) {
            case LEFT_ID:
                tv = titleLeft;
                break;
            case MIDDLE_ID:
                tv = titleText;
                break;
            case RIGHT_ID:
                tv = titleRight;
                break;
            default:
                break;
        }
        return tv;
    }

    /**
     * 设置标题
     *
     * @param textResId 标题文本资源ID
     */
    public void setCommonTitleText(@StringRes int textResId) {
        titleText.setText(textResId);
    }

    /**
     * 设置标题<br/>
     * <p>
     * <b>注意：尽量不要使用这个方式</b>
     * </p>
     *
     * @param text 字符串文本
     */
    public void setCommonTitleText(String text) {
        titleText.setText(text);
    }

    /**
     * 设置标题左侧按钮的文案
     *
     * @param text 按钮文本资源ID
     */
    public void setLeftText(@StringRes int text) {
        titleLeft.setBackgroundColor(Color.TRANSPARENT);
        titleLeft.setText(text);
        titleLeft.setVisibility(View.VISIBLE);
        titleLeftImage.setVisibility(View.GONE);
        isImageVisible = false;
    }

    /**
     * 设置标题右侧按钮的文案
     *
     * @param text 按钮文本资源ID
     */
    public void setRightText(@StringRes int text) {
        titleRight.setBackgroundColor(Color.TRANSPARENT);
        titleRight.setText(text);
        titleRight.setVisibility(View.VISIBLE);
        titleRightImage.setVisibility(View.GONE);
        isImageVisibleRight = false;
    }

    public void setRightText(String  s) {
        titleRight.setBackgroundColor(Color.TRANSPARENT);
        titleRight.setText(s);
        titleRight.setVisibility(View.VISIBLE);
        titleRightImage.setVisibility(View.GONE);
        isImageVisibleRight = false;
    }


    public void setRightTextColor(int color) {
        int c = this.getResources().getColor(color);
        titleRight.setTextColor(c);
    }

    /**
     * 设置标题右侧按钮的文案字体大小
     *
     * @param size 字体大小（DP值）
     */
    public void setRightTextSize(float size) {
        titleRight.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
    }

    /**
     * 设置标题左侧按钮的图标
     *
     * @param resource 按钮图标资源ID
     */
    public void setLeftIcon(@DrawableRes int resource) {
        titleLeftImage.setImageResource(resource);
        titleLeft.setText("");
        titleLeft.setVisibility(View.GONE);
        titleLeftImage.setVisibility(View.VISIBLE);
        isImageVisible = true;
    }

    /**
     * 设置标题右侧按钮的图标
     *
     * @param resource 按钮图标资源ID
     */
    public void setRightIcon(@DrawableRes int resource) {
        titleRightImage.setImageResource(resource);
        titleRight.setText("");
        titleRight.setVisibility(View.GONE);
        titleRightImage.setVisibility(View.VISIBLE);
        isImageVisibleRight = true;
    }

    /**
     * 设置标题左侧按钮的显示
     *
     * @param visible visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
     */
    public void setLeftVisible(int visible) {
        if (!isImageVisible) {
            if (visible == View.VISIBLE)
                titleLeft.setVisibility(View.VISIBLE);
            else
                titleLeft.setVisibility(View.GONE);
        } else {
            if (visible == View.VISIBLE)
                titleLeftImage.setVisibility(View.VISIBLE);
            else
                titleLeftImage.setVisibility(View.GONE);
        }

    }


    /**
     * 设置标题右侧按钮的显示
     *
     * @param visible visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
     */
    public void setRightVisiable(int visible) {
        if (!isImageVisibleRight) {
            if (visible == View.VISIBLE)
                titleRight.setVisibility(View.VISIBLE);
            else
                titleRight.setVisibility(View.GONE);
        } else {
            if (visible == View.VISIBLE)
                titleRightImage.setVisibility(View.VISIBLE);
            else
                titleRightImage.setVisibility(View.GONE);
        }
    }

    public void setOnTitleClickListener(OnTitleClickListener listener) {
        mListener = listener;
    }

    public interface OnTitleClickListener {
        void onCommonTitleClick(int id);
    }

}
